package com.zu.commu.gsm1_0;

import com.zu.commu.util.DateTime;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class DebugSave
{
  private static DebugSave instance;
  private static String path = null;
  private static String degaultPath = "C:/waterGSMTemp/";
  private static File file;

  private DebugSave(String path)
  {
    if (!path.endsWith("/")) {
      path = path + "/";
    }
    path = path;
  }

  public static DebugSave getInstance(String path) {
    if (instance == null) {
      instance = new DebugSave(path);
    }
    instance.init();
    return instance;
  }

  private void init() {
    File dir = new File(path);
    if ((!dir.exists()) && 
      (!dir.mkdirs())) {
      dir = new File(degaultPath);
      if ((!dir.exists()) && 
        (!dir.mkdirs())) {
        return;
      }

    }

    boolean error = false;
    String fileName = path + DateTime.yyyymmdd() + ".txt";
    file = new File(fileName);
    if (!file.exists()) {
      try {
        file.createNewFile();
      } catch (IOException e) {
        error = true;
      }
    }
    if (error)
      file = null;
  }

  public void save(String simNo, String message)
  {
    try {
      FileOutputStream out = new FileOutputStream(file, true);
      String s = simNo + "  " + DateTime.dataTime() + "  " + message + "\n";
      out.write(s.getBytes());
      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] agrs) {
    String simNo = "08613811991765";
    String message = "0x10jkshdfgjlsdfghasdfaldkfasdfa";
    String path = "C:/temp/";
    int count = 10;
    int n = 0;
    while (n < count) {
      DebugSave ds = getInstance(path);
      ds.save(simNo, message);
      n++;
    }
  }
}